package com.controller;

import com.model.OrderRecored;
import com.model.User;
import com.service.OrderMapperService;
import com.sun.org.apache.xerces.internal.xs.StringList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * @author : 虎虎
 * @date :2020/8/4 14:02
 */
@Controller
@RequestMapping("order")
public class OrderRecordController {
    @Autowired
    private OrderMapperService orderMapperService;

    /**
     * 预定申请
     * @param
     */
    @RequestMapping("apply.do")
    @ResponseBody
    public HashMap<String,Object> orderBook(String bookId,HttpServletRequest request){
        OrderRecored order = new OrderRecored();
        order.setBookId(Integer.parseInt(bookId));
        User user = (User) request.getSession().getAttribute("user");

        order.setUserId(user.getUserid());
        order.setUserEmail(user.getEmail());
        int n = orderMapperService.orderBook(order);
        HashMap<String,Object> map = new HashMap<>();
        if(n>0){
            map.put("info","SUCCESS");
        }else{
            map.put("info","FAIL");
        }
        return map;
    }

    /**
     * 返回预定申请数据
     * @return
     */
    @RequestMapping("showApply.do")
    @ResponseBody
    public HashMap<String,Object> showOrderBook(String limit,String page){
        HashMap<String,Object> orderMap = orderMapperService.selectAll(limit,page);
        HashMap<String ,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",orderMap.get("total"));
        map.put("data",orderMap.get("list"));
        return map;
    }
    /**
     * 当用户登录系统并且数据库中存在该书的时候自动向用户发出提示信息
     * 1.判断当前用户是否预定书籍,并且该书籍是否存在
     * 2.有,则返回true
     */
    @RequestMapping("judge.do")
    @ResponseBody
    public HashMap<String,Object> judge(HttpServletRequest request,String flag){
        HashMap<String ,Object> map = new HashMap<>();
        //1.得到登录的用户id
        User user = (User) request.getSession().getAttribute("user");
        Integer id  = user.getUserid();
        //2.查询数据库中是否存在该用户的预定信息
        List<String> stringList = orderMapperService.selectByUserId(id,flag);
        //判断集合长度是否为0
        if(stringList.size()!=0){
            String str = "";
            for (String s : stringList) {
                str += "《"+s+"》 ";
            }
            map.put("stringList",str);
        }else{
            map.put("stringList","null");
        }
        return map;
    }
    /**
     * 1.在用户界面展示预定信息
     */
    @RequestMapping("showOrder.do")
    @ResponseBody
    public HashMap<String,Object> showOrder(HttpServletRequest request,String page,String limit){
        //1.得到登录的用户id
        User user = (User) request.getSession().getAttribute("user");
        Integer id  = user.getUserid();
        //2.查询数据库中是否存在该用户的预定信息
        HashMap<String,Object> orderMap = orderMapperService.selectOrderByUserId(id,limit,page);
        HashMap<String ,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",orderMap.get("total"));
        map.put("data",orderMap.get("list"));
        return map;
    }
    /**
     * 1.处理预定记录
     */
    @RequestMapping("delOrder.do")
    @ResponseBody
    public String delOrder(String orderId){
        //2.查询数据库中是否存在该用户的预定信息
        int n  = orderMapperService.delOrderByOrderId(Integer.parseInt(orderId));
        if (n>0){
            return "SUCCESS";
        }
        return "FAIL";
    }
}